(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('apf.finance.rejectController', createRejectController);
	createRejectController.$inject = [ '$q','$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'piDiscountService','$filter' ];
	function createRejectController($q, $scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, piDiscountService, $filter) {
		var ctrl = this, piDiscountInfo = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.piDiscountRequest = {};

		ctrl.init = function() {
			ctrl.gridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, null);
			ctrl.gridOptions.data = 'vm.piDiscountRequest.discountInvoiceList';
			ctrl.invoiceCriteria = ctrl.gridOptions.defaultSearchCriteria;
			
			authService.createReject(piDiscountInfo).then(function(response) {
				piDiscountService.getCurrencyOptions().then(function(currencyResponse) {
					ctrl.invoiceCcyOptions = currencyResponse.data;
					if (response.data && response.data.model && response.data.task) {
						ctrl.piDiscountRequest = response.data.model;
						ctrl.piDiscountRequest.taskId = response.data.task.taskId;
					}
				});
				
				
			});
		};

		ctrl.createRejectContinue = function() {
			if (!ctrl.piDiscountRequest.discountInvoiceList || ctrl.piDiscountRequest.discountInvoiceList.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			ctrl.piDiscountRequest.invoiceIds = ctrl.piDiscountRequest.invoiceIds || [];
			angular.forEach(ctrl.piDiscountRequest.discountInvoiceList, function(invoice, index) {
				ctrl.piDiscountRequest.invoiceIds.push($filter('encode')(invoice.invoiceId));
			});
			$scope.go('apf.finance.rejectPreview', ctrl.piDiscountRequest);
		};
		
		ctrl.createRejectCancel = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.cancel', args : [ctrl.piDiscountRequest.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.createRejectCancel(ctrl.piDiscountRequest).then(cancelSuccessful, cancelFailed);
					}
				}
			});
		};
		
		function cancelSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.cancel' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('apf.workflow.wfTxn.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'apf.workflow.wfTxn.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.pidiscount.create',
					'state' : 'apf.finance.create',
					'params' : {}
				}]
			});
		}

		function cancelFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}	
		
		ctrl.addInvoice = function(row) {
			return $q(function(resolved, rejected){
				var params = {"multiSelect": "true"};
				params.invoiceIds = [];
				params.dataType = "PI-DISCOUNT";
				angular.forEach(ctrl.piDiscountRequest.discountInvoiceList, function(invoice, index) {
					params.invoiceIds.push($filter('encode')(invoice.invoiceId));
				});
				modalService.openCustomDialogs({
					'dialogsModule' : 'apf.invoice',
					'params' : params,
					'btnCallback' : {
						'ok' : function(selectedRows) {
							if (!selectedRows || selectedRows.length == 0) {
								modalService.openErrorDialogs({
									messages : [ 'title.dialogs.info.norecsel' ]
								});
								return false;
							}
							if (!ctrl.piDiscountRequest.discountInvoiceList) {
								ctrl.piDiscountRequest.discountInvoiceList = [];
							}
							angular.forEach(selectedRows, function(selectedObj, index) {
								selectedRows[index].dataStatus = 'C';
								ctrl.piDiscountRequest.discountInvoiceList.push(selectedRows[index]);
							});
							
							event.preventDefault();
							event.stopPropagation();
						},
						'close' : function(){
							rejected();
						}
					}
				});
			});
			
		};
		
	}
})(angular);